package dashboard.databll;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import net.sf.json.JSONObject;
import dashboard.cache.ServiceBeanFactory;
import dashboard.constant.IncomeNetflowCateConst;
import dashboard.constant.IncomeNetflowCityConst;
import dashboard.dbentity.IncomeCateNetflow;
import dashboard.dbentity.IncomeCityNetflow;
import dashboard.service.AbstractBaseService;
import dashboard.utils.DateUtil;

public class IncomeNetflowBLL {

	private static AbstractBaseService<IncomeCityNetflow> icmCityService = ServiceBeanFactory.get(IncomeCityNetflow.class,null);
	private static AbstractBaseService<IncomeCateNetflow> icmCateService = ServiceBeanFactory.get(IncomeCateNetflow.class,null);
	
	/**
	 * 城市对应的产品(精准，置顶，智能)趋势图数据
	 */
	public static String getCityProJson(String dateGroup, String stat_type,String index, String businessName, String cityName){
		JSONObject ob = new JSONObject();
		String products[] ={"jingzhun","zhiding","zhineng"};
		String cols = IncomeNetflowCityConst.getCityProCol(dateGroup,index);
		String condition =  IncomeNetflowCityConst.getCityProCon(stat_type, businessName, cityName);
//		List<IncomeCityNetflow> list = icmCityService.getListByConditon( cols, condition, dateGroup);
//		TreeMap<String, List<IncomeCityNetflow>> stat_dateMap = ReflectUtil.groupByField(list,dateGroup);

		HashMap<String, List<String>> dataMap = new HashMap<String, List<String>>(); // stat_date : [dataList]
//		for (String date : stat_dateMap.keySet()) {
//			for(String pro : products){
//
//				List<String> curList = dataMap.get(pro);
//				if(curList == null)
//					curList = Lists.newArrayList();
//				IncomeCityNetflow vo = ReflectUtil.getByColVal(IncomeCityNetflow.class,stat_dateMap.get(date),"product_type",pro);
//				curList.add((String) ReflectUtil.getVal(vo, index));
//				dataMap.put(pro, curList);
//			}
//		}
//		ob.element("time", stat_dateMap.keySet());
		
		List<String> dayList = DateUtil.getDateList("2015-01-01", "2015-05-04");
		for(String f : products){
			List<String> dataList = new ArrayList<String>();
			for(int i=0;i<dayList.size();i++){
				dataList.add(String.valueOf(Math.round(Math.random()*1000000)));
			}
			dataMap.put(f, dataList);
		}
		ob.element("time", dayList);
		ob.putAll(dataMap);

		return ob.toString();
	}
	
	/**
	 * echarts点击获取城市对应的产品(精准，置顶，智能) 表格展示数据
	 */
	public static String getCityProTableJson(String dateGroup, String stat_type, String businessName, String dateVal){
//		String condition =  IncomeCityConst.getCityProTableCon(dateGroup, stat_type, businessName, dateVal);
//		List<IncomeCityNetflow> list = icmCityService.getListByConditon( null, condition, "cost desc");

		JSONObject ob = new JSONObject();
//		ob.put("dataList", list);

		return ob.toString();
	}
	

	/**
	 * 产品(精准|置顶|智能)的城市对比趋势图数据
	 */
	public static String getCityCmpJson(String dateGroup, String stat_type,String index, String businessName,String proType, String city1, String city2){
		JSONObject ob = new JSONObject();
		String[] citys={city1,city2};
		String cols = IncomeNetflowCityConst.getCityCmpCol(dateGroup, index);
		String condition =  IncomeNetflowCityConst.getCityCmpCon(stat_type, businessName,proType,city1,city2);
//		List<IncomeCityNetflow> list = icmCityService.getListByConditon( cols, condition, null);
//		TreeMap<String, List<IncomeCityNetflow>> stat_dateMap = ReflectUtil.groupByField(list,dateGroup);

		HashMap<String, List<String>> dataMap = new HashMap<String, List<String>>(); // key : [dataList]
//		for (String date : stat_dateMap.keySet()) {
//			for (String ct : citys) {
//				List<String> curList = dataMap.get(ct);
//				if(curList == null)
//					curList = Lists.newArrayList();
//				IncomeCityNetflow cityVo = ReflectUtil.getByColVal(IncomeCityNetflow.class,stat_dateMap.get(date),"city_name",ct);
//				curList.add((String) ReflectUtil.getVal(cityVo, index));
//				dataMap.put(ct, curList);
//			}
//		}
//		ob.element("time", stat_dateMap.keySet());
		
		List<String> dayList = DateUtil.getDateList("2015-01-01", "2015-05-04");
		for(String f : citys){
			List<String> dataList = new ArrayList<String>();
			for(int i=0;i<dayList.size();i++){
				dataList.add(String.valueOf(Math.round(Math.random()*1000000)));
			}
			dataMap.put(f, dataList);
		}
		ob.element("time", dayList);
		ob.putAll(dataMap);

		return ob.toString();
	}
	
	

    /**
     * 类别对应的产品(精准，置顶，智能)趋势图数据
     */
    public static String getCateProJson(String dateGroup, String stat_type,String index, String businessName, String cateName){
        JSONObject ob = new JSONObject();
        String products[] ={"jingzhun","zhiding","zhineng"};
        String cols = IncomeNetflowCateConst.getCateProCol(dateGroup,index);
        String condition =  IncomeNetflowCateConst.getCateProCon(stat_type, businessName, cateName);
//      List<IncomeCateNetflow> list = icmCateService.getListByConditon( cols, condition, dateGroup);
//      TreeMap<String, List<IncomeCateNetflow>> stat_dateMap = ReflectUtil.groupByField(list,dateGroup);

        HashMap<String, List<String>> dataMap = new HashMap<String, List<String>>(); // stat_date : [dataList]
//      for (String date : stat_dateMap.keySet()) {
//          for(String pro : products){
//
//              List<String> curList = dataMap.get(pro);
//              if(curList == null)
//                  curList = Lists.newArrayList();
//              IncomeCateNetflow vo = ReflectUtil.getByColVal(IncomeCateNetflow.class,stat_dateMap.get(date),"product_type",pro);
//              curList.add((String) ReflectUtil.getVal(vo, index));
//              dataMap.put(pro, curList);
//          }
//      }
//      ob.element("time", stat_dateMap.keySet());
        
        List<String> dayList = DateUtil.getDateList("2015-01-01", "2015-05-04");
        for(String f : products){
            List<String> dataList = new ArrayList<String>();
            for(int i=0;i<dayList.size();i++){
                dataList.add(String.valueOf(Math.round(Math.random()*1000000)));
            }
            dataMap.put(f, dataList);
        }
        ob.element("time", dayList);
        ob.putAll(dataMap);

        return ob.toString();
    }
    
    /**
     * echarts点击获取类别对应的产品(精准，置顶，智能) 表格展示数据
     */
    public static String getCateProTableJson(String dateGroup, String stat_type, String businessName, String dateVal){
//      String condition =  IncomeCateConst.getCateProTableCon(dateGroup, stat_type, businessName, dateVal);
//      List<IncomeCateNetflow> list = icmCateService.getListByConditon( null, condition, "cost desc");

        JSONObject ob = new JSONObject();
//      ob.put("dataList", list);

        return ob.toString();
    }
    

    /**
     * 产品(精准|置顶|智能)的--类别---对比趋势图数据
     */
    public static String getCateCmpJson(String dateGroup, String stat_type,String index, String businessName,String proType, String cate1, String cate2){
        JSONObject ob = new JSONObject();
        String[] cates={cate1,cate2};
        String cols = IncomeNetflowCateConst.getCateCmpCol(dateGroup, index);
        String condition =  IncomeNetflowCateConst.getCateCmpCon(stat_type, businessName,proType,cate1,cate2);
//      List<IncomeCateNetflow> list = icmCateService.getListByConditon( cols, condition, null);
//      TreeMap<String, List<IncomeCateNetflow>> stat_dateMap = ReflectUtil.groupByField(list,dateGroup);

        HashMap<String, List<String>> dataMap = new HashMap<String, List<String>>(); // key : [dataList]
//      for (String date : stat_dateMap.keySet()) {
//          for (String ct : cates) {
//              List<String> curList = dataMap.get(ct);
//              if(curList == null)
//                  curList = Lists.newArrayList();
//              IncomeCateNetflow cateVo = ReflectUtil.getByColVal(IncomeCateNetflow.class,stat_dateMap.get(date),"cate_name",ct);
//              curList.add((String) ReflectUtil.getVal(cateVo, index));
//              dataMap.put(ct, curList);
//          }
//      }
//      ob.element("time", stat_dateMap.keySet());
        
        List<String> dayList = DateUtil.getDateList("2015-01-01", "2015-05-04");
        for(String f : cates){
            List<String> dataList = new ArrayList<String>();
            for(int i=0;i<dayList.size();i++){
                dataList.add(String.valueOf(Math.round(Math.random()*1000000)));
            }
            dataMap.put(f, dataList);
        }
        ob.element("time", dayList);
        ob.putAll(dataMap);

        return ob.toString();
    }
    
}
